package cn.qyl.joha.controller.customer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.dto.Goods;
import cn.qyl.joha.service.GoodsService;
import cn.qyl.joha.service.OrderService;
import cn.qyl.joha.service.Impl.GoodsServiceImpl;
import cn.qyl.joha.service.Impl.OrderServiceImpl;

@Controller
@RequestMapping("/customer/order")
public class OrderSubmitController {
	
	GoodsService goodsService = new GoodsServiceImpl();
	OrderService orderService = new OrderServiceImpl();

	@RequestMapping("/toSubmitOrder")
	public String toSubmitOrder(int[] ids,ModelMap map,HttpSession session){
		
		//获取购物车
		Map<Integer,Integer> shopCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.SHOP_CAR);
		List<Goods> goodsList = new ArrayList<>();
		//统计总价格
		double totalCount = 0.00;
		for(int id:ids){
			System.out.println(id);
			//根据key获取value值
			int buyNum = shopCar.get(id);
			//根据key获取商品对象
			Goods goods = goodsService.getGoodsById(id);
			//设置商品数量
			goods.setBuyNum(buyNum);
			//获取总价格
			totalCount += goods.getFavorablePrice() * goods.getBuyNum();
			goodsList.add(goods);
		}
		
		map.put("totalCount", totalCount);
		map.put("goodsList", goodsList);
		
		return "pay";
	}
}
